﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Entities;
using DAL;

namespace BLL
{
    public class RouteBUS
    {
        public static DataTable GetAll_Route()
        {
            RouteDAL rtBus = new RouteDAL();
            return rtBus.Get_Route_All();
        }
        public static DataTable Search_Route(string Name)
        {
            RouteDAL rtBus = new RouteDAL();
            string sWhere = "RouteName=N'" + Name + "'";
            string sOrderBy = "";
            return rtBus.Search_Route(sWhere, sOrderBy);
        }
        public static Route Get_Station_ByID(int stationID)
        {
            RouteDAL rtBus = new RouteDAL();
            return rtBus.Get_Route_By_ID(stationID);
        }
        public static bool Insert_Route(Route temp)
        {
            RouteDAL RT_DAL = new RouteDAL();
            return RT_DAL.Insert_Route(temp);
        }
        public static bool Update_Route(Route temp)
        {
            RouteDAL RT_DAL = new RouteDAL();
            return RT_DAL.Update_Route(temp);
        }
        public static bool Delete_Route(Route temp)
        {
            RouteDAL RT_DAL = new RouteDAL();
            return RT_DAL.Delete_Route(temp);
        }

        //Chung
        //Chung
        RouteDAL routeDAL;
        RouteDetailDAL routeDetailDAL;
        public RouteBUS()
        {
            routeDAL = new RouteDAL();
            routeDetailDAL = new RouteDetailDAL();
        }
        public DataSet GetAll_Route1()
        {
            return routeDAL.GetAll_Route1();
        }

        public List<RouteDetail> GetList_RouteDetail(string whereSql)
        {
            return routeDetailDAL.GetList_RouteDetail(whereSql, "StationNumber");
        }
        public List<RouteDetail> GetList_RouteDetail(int routeID)
        {
            return routeDetailDAL.GetList_RouteDetailByRouteID(routeID);
        }

        public static int GetDistance(string TrainID, int StationFrom, int StationTo)
        {
            TrainDAL trainDal = new TrainDAL();
            RouteDetailDAL routeDal = new RouteDetailDAL();
            DistanceDAL distanceDal = new DistanceDAL();

            int sum = 0;

            List<RouteDetail> rs = routeDal.GetList_RouteDetail("RouteID=" + trainDal.GetList_Train_By_ID(TrainID).RouteID.ToString(), "StationNumber");
            bool f = false;
            for (int i = 0; i < rs.Count - 1; i++)
            {
                if (rs[i].StationID == StationFrom)
                {
                    f = true;
                }
                if (f)
                {
                    sum = distanceDal.Get_Distance_By_ID(rs[i].StationID, rs[i + 1].StationID).Distances;
                }
                if (rs[i].StationID == StationTo)
                {
                    break;
                }
            }

            return sum;
        }

        public static List<Station> Search_Start_End_Station(string TrainID)
        {
            StationDAL stationDal = new StationDAL();
            return stationDal.Get_Start_End_Station(TrainID);
        }
        public static DataTable Get_Station_By_RouteID(int RouteID)
        {
            RouteDetailDAL rdDal = new RouteDetailDAL();
            return rdDal.GetList_Station_By_RouteID(RouteID);
        }

        public static bool AddStation_To_RouteDetail(int RouteID, int StationID, int StationNumber)
        {
            RouteDetailDAL rdDal = new RouteDetailDAL();
            return rdDal.AddStation_To_RouteDetail(RouteID, StationID, StationNumber);
        }

        public static bool DeleteStation_From_RouteDetail(int RouteID, int StationID)
        {
            RouteDetailDAL rdDal = new RouteDetailDAL();
            return rdDal.DeleteStation_From_RouteDetail(RouteID, StationID);
        }

        public static bool UpStation_In_RouteDetail(int RouteID, int StationID)
        {
            RouteDetailDAL rdDal = new RouteDetailDAL();
            return rdDal.UpStation_In_RouteDetail(RouteID, StationID);
        }

        public static bool DownStation_In_RouteDetail(int RouteID, int StationID)
        {
            RouteDetailDAL rdDal = new RouteDetailDAL();
            return rdDal.DownStation_In_RouteDetail(RouteID, StationID);
        }
    }
}
